<?php
        require("../inc/ajax.php");
        $LOCKFILE_CREATE= '/var/run/uggla_rdiff_usb_create.lock';
        $BASEDIR="/usr/share/uggla";

	treelist_next();

	if( $_GET['msg'] == 'usbstat' ) {
		exec("$BASEDIR/services/rdiffbackup/usb_rdiff_wwwadmin list", $dev_arr);
		$count = sizeof($dev_arr);
		
		if( isset($dev_arr['0']) AND ($dev_arr['0'] == "no connected") ) {
			if( is_file($LOCKFILE_CREATE) )
				echo "creating";
			else
				echo "0";
		}
		else {
			exec("$BASEDIR/services/rdiffbackup/usb_rdiff_wwwadmin device", $device);
			if( isset($device[0]) AND ($device[0] == "not connected") ) {
				$type = explode('^', $dev_arr[0]);
				$vendor = $type[1];
				$model = $type[2];
				// antal anslutna ^ är backupenhet ^ vendor ^ model
				echo "$count^false^$vendor^$model";
			}
			else {
                                if( isset($device[0]) ) {
                                    $type = explode('^', $device[0]);
                                    $mpoint = $type[1];
                                    $vendor = $type[2];
                                    $model = $type[3];

                                    function human_space($space) {
                                            if( $space < 1000)
                                                    return "$space B";
                                            else {
                                                    $space = $space / 1000;
                                                    if( $space < 1000)
                                                            return round($space)." KB";
                                                    else {
                                                            $space = $space / 1000;
                                                            if( $space < 1000)
                                                                    return round($space)." MB";
                                                            else {
                                                                    $space = $space / 1000;
                                                                    return round($space)." GB";
                                                            }
                                                    }
                                            }
                                    }
                                    $space_total = human_space(disk_total_space($mpoint));
                                    $space_used_raw = disk_total_space($mpoint) - disk_free_space($mpoint);
                                    $space_used = human_space($space_used_raw) . " (" . round($space_used_raw/disk_total_space($mpoint)*100)."%)";

                                    // antal anslutna ^ är backupenhet ^ vendor ^ model ^ är backupenhet ^ totalt utrymme ^ ledigt utrymme
                                    echo "$count^true^$vendor^$model^$space_total^$space_used";
                                }
                                else {
                                    echo "^true^^^^";
                                }
                            
			}
			
		}
	}
?>
